import 'dart:convert';

import 'package:fish_redux/fish_redux.dart';
import 'package:json_annotation/json_annotation.dart';

@JsonSerializable()
class PagedResult<T> extends Cloneable<PagedResult> {
  List<T> items;
  int total;

  PagedResult({
    this.items,
    this.total,
  });

  @override
  PagedResult clone() {
    return PagedResult(
      items: items,
      total: total,
    );
  }

  factory PagedResult.fromJson(
      Map<String, dynamic> json, T Function(Object) fromJson) {
    return PagedResult(
      total: json['total'],
      items: List<T>.from(json['items'].map((x) => fromJson(x))),
    );
  }

  Map<String, dynamic> toJson() => {
        'total': total,
        'items': jsonEncode(items),
      };
}
